/*
 * Copyright (C) 2004, Velocityme Partnership
 * DepartmentActions.java
 *
 * Created on 05 October 2004, 05:28
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.Department;
import com.velocityme.session.ContactDetailLocal;
import com.velocityme.session.DepartmentLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.www.actionforms.nodeactionforms.DepartmentActionForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class DepartmentActions extends ContactableActions {

    /** Creates a new instance of DepartmentActions */
    public DepartmentActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("department", lookupDepartmentBean().findDepartment(key, nodeId));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showCreate(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Department department = new Department();
        department.setParentNode(lookupNodeBean().findNode(key, nodeId));
        session.setAttribute("department", department);
        DepartmentActionForm departmentAF = new DepartmentActionForm();
        createContactDetails(key, departmentAF);
        session.setAttribute("departmentAF", departmentAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Department department = lookupDepartmentBean().findDepartment(key, nodeId);
        session.setAttribute("department", department);
        DepartmentActionForm departmentValueAF = new DepartmentActionForm();
        editContactDetails(key, department, departmentValueAF);
        session.setAttribute("departmentAF", departmentValueAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Long nodeId;
        if (request.getParameter("btnSubmit").equals("OK")) {
            Department department = (Department)session.getAttribute("department");
            ((DepartmentActionForm)form).extractDepartment(key, department);
            if (department.getId() != null) {
                lookupDepartmentBean().editDepartment(key, department);
            }
            else {
                department = lookupDepartmentBean().createDepartment(key, department);
            }
            nodeId = department.getId();
        }
        // Cancel simply displays the original node again
        else {
            nodeId = (Long)session.getAttribute("nodeId");
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private DepartmentLocal lookupDepartmentBean() {
        try {
            Context c = new InitialContext();
            return (DepartmentLocal) c.lookup("java:comp/env/DepartmentBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ContactDetailLocal lookupContactDetailBean() {
        try {
            Context c = new InitialContext();
            return (ContactDetailLocal) c.lookup("java:comp/env/ContactDetailBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
